package com.example.composableexample


import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.VectorConverter
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.keyframes
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.colorspace.ColorSpaces
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

/**
 * author: LiLiang
 * createTime: 2022/9/30-9:40
 * remark:
 **/

@Preview
@Composable
fun AnimatedVisibilitySimple() {

    CustomCanvasAnimation()

//    val showText by remember {
//        mutableStateOf(true)
//    }
//
//    AnimatedVisibility(visible = showText) {
//        RoundUp(roundUp = showText, roundUpChange = {})
//    }
}

@Composable
fun CustomCanvasAnimation() {
    val angle = remember {
        Animatable(0f)
    }
    val color = remember {
        Animatable(Color.Green, typeConverter = Color.VectorConverter(ColorSpaces.LinearSrgb))
    }
    LaunchedEffect(angle, color) {
        launch {
            angle.animateTo(360F, animationSpec = tween(3000))
        }
        launch {
//            color.animateTo(Color.Blue, animationSpec = tween(3000))
            color.animateTo(Color.Blue, animationSpec = keyframes {
                durationMillis = 3000
                Color.Red at 500 with LinearEasing
                Color.Yellow at 1000 with FastOutSlowInEasing
                Color.Gray at 1500 with LinearOutSlowInEasing
                Color.Transparent at 2000 with FastOutSlowInEasing
                Color.White at 2500 with FastOutLinearInEasing
            })
        }
    }
    Canvas(modifier = Modifier.size(200.dp), onDraw = {
        rotate(angle.value) {
            drawRoundRect(
                color = color.value,
                cornerRadius = CornerRadius(16.dp.toPx())
            )
        }
    })
}
